// pages/buy/buy.js
const app = getApp()
var host = app.globalData.host;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressId: '',
        addresses: '',
        flag: 0,
        goodsDetail: null,
        goodsId: '',
        num: 1,
        totalPrice: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {
        console.log(e);
        var that = this;
        this.loadGoods(e.goodsId);
        this.setData({ goodsId: e.goodsId });
        this.setData({ num: e.num });
        this.setData({ addressId: e.addressId });
        //需要有loadAddress这个函数，才可以拿到数据
        this.loadAddress(e.addressId);
    },

    loadGoods: function(goodsId) {
        var page = this;
        if (goodsId != null && goodsId != '') {
            wx.request({
                url: host + '/api/goods/getGoodsDetail',
                method: 'GET',
                data: {
                    "goodsId": goodsId
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res);
                    var code = res.data.code;

                    var goodsDetail = res.data.data;
                    console.log(goodsDetail);
                    if (code = '0000') {
                        page.setData({ goodsDetail: goodsDetail });
                        // console.log(page.data);
                        var num = page.data.num;
                        //计算总价格
                        var totalPrice = goodsDetail.goodsPrice * num;
                        page.setData({ totalPrice: totalPrice.toFixed(2) });
                        console.log(totalPrice);
                        console.log(num);
                    }
                }
            })
        }
    },

    loadAddress: function(id) {
        var page = this;
        if (id != null && id != "") {
            wx.request({
                // url: host + '/api/address/selectAddressByUserId',根据用户id来循环，之前address.js写的
                //这里是需要通过地址id
                url: host + '/api/address/getAddressById',
                method: 'GET',
                data: {
                    // "userId": userId
                    "id": id
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res)
                    var code = res.data.code;
                    var addresses = res.data.data;
                    if (code = '0000') {
                        page.setData({ addresses: addresses });
                    }
                    console.log(addresses)
                }
            })
        } else {
            // wx.redirectTo({
            //     url: '../login/login'
            // })
        }
    },
    selectAddress: function() { //选择收货地址
        wx.navigateTo({
            url: '../address/address?goodsId=' + this.data.goodsId + "&num=" + this.data.num
        })
    },
    buy: function() { //立即购买
        var userId = wx.getStorageSync("userId");
        var addressId = this.data.addressId;
        var goodsId = this.data.goodsId;
        var num = this.data.num;
        var totalPrice = this.data.totalPrice;

        if (addressId != '' && addressId != null) {
            //保存订单信息
            wx.request({
                url: host + '/api/order/saveOrder',
                method: 'GET',
                data: {
                    "goodsId": goodsId,
                    "userId": userId,
                    "addressId": addressId,
                    "num": num
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res);
                    var code = res.data.code;
                    var orderId = res.data.data; //订单号
                    if (code = '0000') {
                        wx.navigateTo({ //支付成功后跳转成功页
                            url: '../paySuccess/paySuccess?orderId=' + orderId
                        })
                    }
                }
            })

        } else {
            wx.showModal({
                title: '提示',
                content: '请选择收货地址',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        console.log('用户点击确定')
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    },



})